package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.ArticlesReqVo;
import com.indexingsystem.boss.Vo.ArticlesTableVo;
import com.indexingsystem.boss.Vo.ArticlesVo;
import com.indexingsystem.boss.Vo.ResCategoryRelationsVo;
import com.indexingsystem.boss.admin.mapper.ArticleContentsMapper;
import com.indexingsystem.boss.admin.mapper.ArticlesMapper;
import com.indexingsystem.boss.admin.mapper.AuditLogsMapper;
import com.indexingsystem.boss.admin.mapper.DhMagazinesMlMapper;
import com.indexingsystem.boss.admin.mapper.DhResourceauditlogsMapper;
import com.indexingsystem.boss.admin.mapper.ResCategoryRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourceRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourcesMapper;
import com.indexingsystem.boss.admin.mapper.SpecialResourceDatasMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.ArticleContents;
import com.indexingsystem.boss.entity.Articles;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.ResCategoryRelations;
import com.indexingsystem.boss.entity.ResourceRelations;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.DateUtil;
import com.indexingsystem.boss.utils.InstitutionLoadUtils;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.db.mongoDB.impl.ArticlesResourceMongo;
import com.indexingsystem.db.mongoDB.po.ArticlesResourceMongoVo;
import com.indexingsystem.system.service.IArticlesService;
import com.indexingsystem.system.service.IResourceCategorsService;
import com.indexingsystem.system.service.IdhTodayStorageInfosService;

@Service
public class ArticlesServiceImpl implements IArticlesService {

	@Resource
	private ArticlesMapper				articlesMapper;
	@Resource
	private ResourcesMapper				resourcesMapper;
	@Resource
	private ResCategoryRelationsMapper	resCategoryRelationsMapper;
	@Resource
	private SpecialResourceDatasMapper	specialResourceDatasMapper;

	@Resource
	private AuditLogsMapper				auditLogsMapper;

	@Resource
	private IdhTodayStorageInfosService	todayStorageInfosDao;

	@Resource
	private DhResourceauditlogsMapper	dhResourceauditlogsMapper;

	@Resource
	private ArticleContentsMapper		articleContentsMapper;

	@Resource
	private ResourceRelationsMapper		resourceRelationsMapper;

	@Resource
	private DhMagazinesMlMapper			dhMagazinesMlMapper;

	@Resource
	private IResourceCategorsService	iResourceCategorsService;

	@Override
	public int deleteByPrimaryKey(String resId) {
		return articlesMapper.deleteByPrimaryKey( resId );
	}

	@Override
	public int insert(Articles record) {
		return articlesMapper.insert( record );
	}

	@Override
	public int insertSelective(Articles record) {
		return articlesMapper.insertSelective( record );
	}

	@Override
	public Articles selectByPrimaryKey(String resId) {
		return articlesMapper.selectByPrimaryKey( resId );
	}

	@Override
	public int updateByPrimaryKeySelective(Articles record) {
		return articlesMapper.updateByPrimaryKeySelective( record );
	}

	@Override
	public List<ArticlesVo> searchArticlesList(ArticlesVo record) {
		return articlesMapper.searchArticlesList( record );
	}

	@Override
	public Integer getArticlesListCount(ArticlesVo record) {
		Integer maxCount = 0;
		maxCount = ArticlesResourceMongo.findArticlesMaxCount( record, InstitutionLoadUtils.getString( "institutionId" ) );
		if (maxCount == 0) {
			maxCount = articlesMapper.getArticlesListCount( record );
		}
		return maxCount;
	}

	@Override
	public PagedResult<ArticlesTableVo> selectArticlesList(ArticlesVo articlesVo) {
		Integer pageNo = articlesVo.getPageNumber() == null ? 1 : articlesVo.getPageNumber();
		Integer pageSize = articlesVo.getPageSize() == null ? 10 : articlesVo.getPageSize();
		List<ArticlesTableVo> articlesTableVoList = ArticlesResourceMongo.findArticlesByPage( articlesVo, InstitutionLoadUtils.getString( "institutionId" ) );
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		if (null != articlesTableVoList && articlesTableVoList.size() > 0) {
			return BeanUtils.toPagedResult( articlesTableVoList );
		} else {
			return BeanUtils.toPagedResult( articlesMapper.selectArticlesList( articlesVo ) );
		}
	}

	@Override
	public int addArticlesInfo(ArticlesReqVo articlesReqVo, AuditLogs auditLogs, DhTodayStorageInfos dhTodayStorageInfos) throws Exception {
		int result = 0;
		if (null != articlesReqVo) {
			ArticlesResourceMongoVo articlesResourceMongoVo = new ArticlesResourceMongoVo();
			Resources resources = articlesReqVo.getResources();
			if (null != resources) {
				articlesResourceMongoVo.setResId( resources.getResId() );
				articlesResourceMongoVo.setResTypeId( resources.getResTypeId() );
				articlesResourceMongoVo.setResTable( "Articles" );

				resourcesMapper.insertSelective( resources );
				articlesResourceMongoVo.setResources( resources );
			}
			Articles articles = articlesReqVo.getArticles();
			if (null != articles) {
				result = articlesMapper.insertSelective( articles );
				articlesResourceMongoVo.setDhArticles( articles );

				// 新增文章内容表
				ArticleContents articleContents = new ArticleContents();
				articleContents.setResId( articles.getResId() );
				articleContents.setContent( articles.getDescription() );
				articleContentsMapper.insertSelective( articleContents );
				articlesResourceMongoVo.setDhArticleContents( articleContents );
			}
			ResCategoryRelations resCategoryRelation = null;
			List<ResCategoryRelations> resultResCategoryRelationsList = null;
			List<ResCategoryRelationsVo> resCategoryRelationsVoList = articlesReqVo.getResCategoryRelationsList();
			if (null != resCategoryRelationsVoList && resCategoryRelationsVoList.size() > 0) {
				resultResCategoryRelationsList = new ArrayList<ResCategoryRelations>();
				for (ResCategoryRelationsVo resCategoryRela : resCategoryRelationsVoList) {
					resCategoryRelation = new ResCategoryRelations();
					resCategoryRelation.setResCategoryRelationId( resCategoryRela.getResCategoryRelationId() );
					resCategoryRelation.setResCategoryId( resCategoryRela.getResCategoryId() );
					resCategoryRelation.setResId( resCategoryRela.getResId() );

					if (!resultResCategoryRelationsList.contains( resCategoryRelation )) {
						resultResCategoryRelationsList.add( resCategoryRelation );
					}
				}
			}
			if (null != resultResCategoryRelationsList && resultResCategoryRelationsList.size() > 0) {
				resCategoryRelationsMapper.insertResCategoryRelationsBatch( resultResCategoryRelationsList );
				articlesResourceMongoVo.setResCategoryRelationsList( resultResCategoryRelationsList );
			}
			List<SpecialResourceDatas> specialResourceDatasList = articlesReqVo.getSpecialResourceDatasList();
			if (null != specialResourceDatasList && specialResourceDatasList.size() > 0) {
				specialResourceDatasMapper.batchAddSpecialResourceDatas( specialResourceDatasList );

				articlesResourceMongoVo.setDhSpecialResourceDatasList( specialResourceDatasList );
			}
			auditLogsMapper.insertSelective( auditLogs );
			articlesResourceMongoVo.setAuditLogs( auditLogs );

			// 新增入库审核日志
			DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
			// 增加审核信息
			resourceauditlogs.setCreatedate( DateUtil.parseFormatToDate( DateUtil.getCurTotalDate() ) );
			resourceauditlogs.setHandler( auditLogs.getHandler() == null ? "admin" : auditLogs.getHandler() );
			resourceauditlogs.setResid( articles.getResId() );
			resourceauditlogs.setId( StringUtil.createSystemDataPrimaryKey() );
			resourceauditlogs.setStatus( 1 );
			resourceauditlogs.setDescription( "新增文章入库" );
			dhResourceauditlogsMapper.insert( resourceauditlogs );
			articlesResourceMongoVo.setResourceauditlogs( resourceauditlogs );

			// 新增流水
			todayStorageInfosDao.insert( dhTodayStorageInfos );
			articlesResourceMongoVo.setTodayStorageInfos( dhTodayStorageInfos );
			ArticlesResourceMongo.addArticles( articlesResourceMongoVo, InstitutionLoadUtils.getString( "institutionId" ) );
		}
		return result;
	}

	@Override
	public int updateArticlesInfo(ArticlesReqVo articlesReqVo) throws Exception {
		int result = 0;
		if (null != articlesReqVo) {
			ArticlesResourceMongoVo articlesResourceMongoVo = new ArticlesResourceMongoVo();
			Resources resources = articlesReqVo.getResources();
			if (null != resources) {
				articlesResourceMongoVo.setResId( resources.getResId() );
				articlesResourceMongoVo.setResTypeId( resources.getResTypeId() );
				articlesResourceMongoVo.setResTable( "Articles" );
				resourcesMapper.updateByPrimaryKeySelective( resources );
				Resources queryRes = resourcesMapper.selectByPrimaryKey( resources.getResId() );
				articlesResourceMongoVo.setResources( queryRes );
			}
			Articles articles = articlesReqVo.getArticles();
			if (null != articles) {
				result = articlesMapper.updateByPrimaryKeySelective( articles );
				Articles queryArticles = articlesMapper.selectByPrimaryKey( articles.getResId() );
				articlesResourceMongoVo.setDhArticles( queryArticles );

				// 修改文章内容表
				ArticleContents articleContents = new ArticleContents();
				articleContents.setResId( articles.getResId() );
				articleContents.setContent( articles.getDescription() );
				articleContentsMapper.updateByPrimaryKeySelective( articleContents );
				articlesResourceMongoVo.setDhArticleContents( articleContents );

			}
			ResCategoryRelations resCategoryRelation = null;
			List<String> resIdList = null;
			List<String> resCategoryIdList = null;
			List<ResCategoryRelations> resultResCategoryRelationsList = null;
			List<ResCategoryRelationsVo> resCategoryRelationsVoList = articlesReqVo.getResCategoryRelationsList();
			if (null != resCategoryRelationsVoList && resCategoryRelationsVoList.size() > 0) {
				resIdList = new ArrayList<String>();
				resCategoryIdList = new ArrayList<String>();
				resultResCategoryRelationsList = new ArrayList<ResCategoryRelations>();
				for (ResCategoryRelationsVo resCategoryRela : resCategoryRelationsVoList) {
					resCategoryRelation = new ResCategoryRelations();
					resCategoryRelation.setResCategoryRelationId( StringUtil.createSystemDataPrimaryKey() );
					resCategoryRelation.setResCategoryId( resCategoryRela.getResCategoryId() );
					resCategoryRelation.setResId( resCategoryRela.getResId() );
					resCategoryIdList.add( resCategoryRela.getResCategoryId() );
					resIdList.add( resCategoryRela.getResId() );

					if (!resultResCategoryRelationsList.contains( resCategoryRelation )) {
						resultResCategoryRelationsList.add( resCategoryRelation );
					}
				}
			}

			List<SpecialResourceDatas> specialResourceDatasList = articlesReqVo.getSpecialResourceDatasList();
			if (null != specialResourceDatasList && specialResourceDatasList.size() > 0) {
				if (null != resIdList && resIdList.size() > 0) {
					// 终端先删后加
					specialResourceDatasMapper.deleteSpecialResourceDatasByIdBatch( resIdList );
				}
				specialResourceDatasMapper.batchAddSpecialResourceDatas( specialResourceDatasList );
				articlesResourceMongoVo.setDhSpecialResourceDatasList( specialResourceDatasList );
			}

			if (null != resIdList && resIdList.size() > 0) {
				// 先删后加
				resCategoryRelationsMapper.deleteResBatchCategoryRelationsByResId( resIdList );
				if (null != resultResCategoryRelationsList && resultResCategoryRelationsList.size() > 0) {
					// 后加
					resCategoryRelationsMapper.insertResCategoryRelationsBatch( resultResCategoryRelationsList );
					articlesResourceMongoVo.setResCategoryRelationsList( resultResCategoryRelationsList );
				}
			}
			AuditLogs auditLogs = null;
			List<AuditLogs> auditLogsList = auditLogsMapper.getAuditLogsListByResId( articlesReqVo.getResources().getResId() );
			if (null != auditLogsList && auditLogsList.size() > 0) {
				auditLogs = auditLogsList.get( 0 );
			}
			articlesResourceMongoVo.setAuditLogs( auditLogs );

			DhResourceauditlogs dhResourceAuditLogs = dhResourceauditlogsMapper.getResAuditlogsByResId( articlesReqVo.getResources().getResId() );
			articlesResourceMongoVo.setResourceauditlogs( dhResourceAuditLogs );

			DhTodayStorageInfos dhTodayStorageInfos = null;
			List<DhTodayStorageInfos> todayStorageInfosList = todayStorageInfosDao.getDhTodayStorageInfosByResId( articlesReqVo.getResources()
					.getResId() );
			if (null != todayStorageInfosList && todayStorageInfosList.size() > 0) {
				dhTodayStorageInfos = todayStorageInfosList.get( 0 );
			}
			articlesResourceMongoVo.setTodayStorageInfos( dhTodayStorageInfos );
			ArticlesResourceMongo.updateArticles( articlesResourceMongoVo, InstitutionLoadUtils.getString( "institutionId" ) );
		}

		return result;
	}

	@Override
	public int deleteArticlesByIdBatch(List<String> resIdList) throws Exception {
		int result = 0;
		if (null != resIdList && resIdList.size() > 0) {
			for (String resId : resIdList) {
				Articles queryArticles = selectByPrimaryKey( resId );
				if (null != queryArticles) {
					// 1、删除
					result += deleteByPrimaryKey( queryArticles.getResId() );

					// 获取订单文章内容表
					// 添加文章内容列表
					ArticleContents articleContents = articleContentsMapper.selectByPrimaryKey( queryArticles.getResId() );
					if (null != articleContents) {
						articleContentsMapper.deleteByPrimaryKey( articleContents.getResId() );
					}

					// 第三步删除资源关系
					// 资源关系列表
					List<ResourceRelations> queryResRelationsList = resourceRelationsMapper
							.getResourceRelationsListByResId( queryArticles.getResId() );
					for (ResourceRelations resRelations : queryResRelationsList) {
						resourceRelationsMapper.deleteByPrimaryKey( resRelations.getResRelationId() );
					}

					// 第四步：删除资源终端关系
					List<SpecialResourceDatas> tempSpecialResDataList = specialResourceDatasMapper.getSpecialResourceDataListByResId( queryArticles
							.getResId() );
					for (SpecialResourceDatas resSpecialDatas : tempSpecialResDataList) {
						specialResourceDatasMapper.deleteBySpecialResourceDataId( resSpecialDatas.getSpecialResDataId() );
					}

					// 5、删除审核日志表关系
					List<AuditLogs> auditLogsList = auditLogsMapper.getAuditLogsListByResId( queryArticles.getResId() );
					for (AuditLogs auditLogs : auditLogsList) {
						auditLogsMapper.deleteByPrimaryKey( auditLogs.getProductLogId() );
					}

					// 清理入库审核日志表关系
					DhResourceauditlogs dhResourceAuditLogs = dhResourceauditlogsMapper.getResAuditlogsByResId( queryArticles.getResId() );
					if (null != dhResourceAuditLogs) {
						dhResourceauditlogsMapper.deleteByPrimaryKey( dhResourceAuditLogs.getId() );
					}

					// 删除流水
					List<DhTodayStorageInfos> dhTodayStorageInfosList = todayStorageInfosDao.getDhTodayStorageInfosByResId( queryArticles.getResId() );
					for (DhTodayStorageInfos dhTodayStorageInfos : dhTodayStorageInfosList) {
						todayStorageInfosDao.deleteTodayStorageInfoById( dhTodayStorageInfos.getStorageId() );
					}

					resourcesMapper.deleteByPrimaryKey( queryArticles.getResId() );

					// 批量删除mongo文章缓存记录，通过resId in操作获取，批量删除
					ArticlesResourceMongo.deleteArticlesByIds( resIdList, InstitutionLoadUtils.getString( "institutionId" ) );

				}

			}

		}

		return result;
	}
}